package com.kaibai.controller;

import com.kaibai.pojo.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class loginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        HttpSession session = servletRequest.getSession();
        String path = servletRequest.getServletPath();
        if(session.getAttribute("user") == null && !path.contains("login") && !path.contains("register") && !path.contains("login1") && !path.contains("404")){
            //servletRequest.getRequestDispatcher("/jsp/user/login.jsp").forward(request,response);
            servletRequest.getRequestDispatcher("/jsp/user/login1.jsp").forward(request,response);
            return;
        }else{
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
